﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using Sebarf.Core.Interfaces;
using Sebarf.Diagnostics.Interfaces;
using Sebarf.Services.Interfaces;

#endregion

namespace InterLinq.Test.UI {
	/// <summary>
	/// Interaction logic for PagedProductsView.xaml
	/// </summary>
	public partial class ServicesView : UserControl {
		#region Public Methods

		private ServicesView() {
			InitializeComponent();
		}

		public ServicesView(IServiceLocatorService data)
			: this() {
			Check.ArgumentNotNull(data, "data");
			m_data = data;
			m_availableServices =
				data.GetRegisteredService<IDiagnosticsServiceLocatorService>().GetAllRegisteredServices<IMonitorableService>();
			m_items.ItemsSource = m_availableServices.Select(x => x.CollectAndGetServiceStatistics());
		}

		#endregion

		#region Private Fields

		private readonly IMonitorableService[] m_availableServices;
		private IServiceLocatorService m_data;

		#endregion

		private void m_items_MouseUp(object sender, MouseButtonEventArgs e) {
			var selectedService = m_items.SelectedItem as ServiceStatistics;
			m_detailItems.ItemsSource = null;
			m_dependencies.ItemsSource = null;

			if (selectedService == null) {
			}
			else {
				selectedService = selectedService.UnderlyingService.CollectAndGetServiceStatistics();
				m_detailItems.ItemsSource = selectedService.ServiceStatisticEntries;
				m_name.Content = selectedService.ServiceName;
				m_friendlyName.Content = selectedService.FriendlyServiceName;
				m_dependencies.ItemsSource = selectedService.ReferencedServices.Select(x => x.GetType().Name);
				m_time.Content = selectedService.CollectedDate;
			}
		}
	}
}